package com.tisemi.mes;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.deer.wms.base.system.dao.bill.BillInSapDetailMapper;
import com.deer.wms.base.system.dao.bill.BillInSapLogMapper;
import com.deer.wms.base.system.dao.bill.BillInSapMapper;
import com.deer.wms.base.system.finalUtil.BeanContextUtils;
import com.deer.wms.base.system.finalUtil.SapStateConst;
import com.deer.wms.base.system.model.bill.BillInSap;
import com.deer.wms.base.system.model.bill.BillInSapDetail;
import com.deer.wms.base.system.model.bill.BillInSapLog;
import com.deer.wms.base.system.webservice.model.SapToWMSModel;
import com.deer.wms.base.system.webservice.model.WMSToSapModel;
import com.deer.wms.base.system.webservice.model.saprequest.ConstantTrans;
import com.deer.wms.base.system.webservice.model.saprequest.HEAD;
import com.deer.wms.base.system.webservice.model.saprequest.ITEM;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.List;

/**
 * @author 申紫微
 * @version 1.0
 * @description
 * @data 2021-07-18 21:25:23
 */
@Slf4j
public class KW_TO_SAP_WebServiceSoapImpl implements KWTOSAPWebServiceSoap{

    @Autowired
    BillInSapLogMapper billInSapLogMapper;

    @Autowired
    BillInSapMapper sapMapper;

    @Autowired
    BillInSapDetailMapper sapDetailMapper;

    @Override
    public ESOUTPUT sapTORequestResponse(String sysid, String ifywid, String zdata, String bskey, String sapkey, String zorg, String zfiled1, String zfiled2, String zfiled3, String zfiled4, String zfiled5) {


        ESOUTPUT esoutput = ConvertSAPMsgToModel(zdata, ifywid, bskey, sapkey, zorg, zdata);
        return esoutput;
    }

    @Transactional
    public ESOUTPUT ConvertSAPMsgToModel(String sapZData,String ifywid,String bskey, String sapkey, String zorg,String zdata){
        SapToWMSModel sapRequestModel = null;
        sapRequestModel = JSONObject.parseObject(sapZData, SapToWMSModel.class);
        ESOUTPUT esoutput = new ESOUTPUT();
        if (sapRequestModel == null || sapRequestModel.getHEAD() == null || sapRequestModel.getITEM() == null) {
            log.error("SAP下发的单据为空，转换失败");
            WMSToSapModel backToSapModel = new WMSToSapModel();
            backToSapModel.setVBELN("");
            backToSapModel.setERRORNO(ConstantTrans.ERRORNO_SUEECSS);
            backToSapModel.setERRORMSG(ConstantTrans.ERRORMSG_SUEECSS);
            String backMsg = JSONObject.toJSONString(backToSapModel);
            esoutput.setZDATA(backMsg);
            esoutput.setZMESSAGE("sucess");
            esoutput.setZTYPE("正常");
            return esoutput;
        }
        List<HEAD> headList = sapRequestModel.getHEAD();
        HEAD head = headList.get(0);
        String vbeln = head.getVBELN();
        WMSToSapModel backToSapModel = new WMSToSapModel();
        backToSapModel.setVBELN(vbeln);
        backToSapModel.setERRORNO(ConstantTrans.ERRORNO_SUEECSS);
        backToSapModel.setERRORMSG(ConstantTrans.ERRORMSG_SUEECSS);
        String backMsg = JSONObject.toJSONString(backToSapModel);
        esoutput.setZDATA(backMsg);
        esoutput.setZMESSAGE("sucess");
        esoutput.setZTYPE("正常");
        BillInSapLog billInSapLog = new BillInSapLog();
        // 除真实报文外的其他数据信息
        billInSapLog.setIFYWID(ifywid);
        billInSapLog.setBSKEY(bskey);
        billInSapLog.setSAPKEY(sapkey);
        billInSapLog.setZORG(zorg);
        billInSapLog.setZDATA(zdata);
        // todo APIRESULT  Status ExceMation 科沃系统导入的数据存在该值，我方根据实际情况调整
        billInSapLog.setVBELN(head.getVBELN());
        billInSapLog.setERRORNO(ConstantTrans.ERRORNO_SUEECSS);
        billInSapLog.setERRORMSG(ConstantTrans.ERRORMSG_SUEECSS);
        String now = DateUtil.now();
        billInSapLog.setCreateTime(now);
        billInSapLogMapper = BeanContextUtils.getApplicationContext().getBean(BillInSapLogMapper.class);
        int insert = billInSapLogMapper.insertBillInSapLog(billInSapLog);
        boolean res = SaveSapSendData(sapRequestModel);
        if (insert <= 0 || !res){
            log.error("SAP下发的单据插入日志失败");
            WMSToSapModel backToSapModels = new WMSToSapModel();
            backToSapModels.setVBELN("");
            backToSapModels.setERRORNO(ConstantTrans.ERRORNO_SUEECSS);
            backToSapModels.setERRORMSG(ConstantTrans.ERRORMSG_SUEECSS);
            String backMsgError = JSONObject.toJSONString(backToSapModels);
            esoutput.setZDATA(backMsgError);
            esoutput.setZMESSAGE("sucess");
            esoutput.setZTYPE("正常");
            return esoutput;
        }
        return esoutput;
    }

    /**
     * 保存SAP单据信息
     * @author 申紫微
     * @date 23:24 2021/7/18
     * @params model
     * @return boolean
     **/
    @Transactional
    public boolean SaveSapSendData(SapToWMSModel model){
        sapMapper = BeanContextUtils.getApplicationContext().getBean(BillInSapMapper.class);
        sapDetailMapper = BeanContextUtils.getApplicationContext().getBean(BillInSapDetailMapper.class);
        List<HEAD> head = model.getHEAD();
        if (head == null){
            return false;
        }
        HEAD headModel = head.get(0);
        BillInSap billInSap = new BillInSap();
//        billInSap.setLFART(headModel.getLFART());
//        billInSap.setVTEXT(headModel.getVTEXT());
//        billInSap.setVBELN(headModel.getVBELN());
//        billInSap.setLIFNR(headModel.getLIFNR());
//        billInSap.setBUKRS(headModel.getBUKRS());
//        billInSap.setEKORG(headModel.getEKORG());
//        billInSap.setEKGRP(headModel.getEKGRP());
//        billInSap.setITEMTEXT(headModel.getITEMTEXT());
        String now = DateUtil.now();
        billInSap.setCreateTime(now);
        billInSap.setBillState(SapStateConst.BILL_IN_SAP_STATE_AUDITED);
        int insert = sapMapper.saveBillInSap(billInSap);
        if (insert != 1){
            return false;
        }
        List<ITEM> item = model.getITEM();
        for (ITEM entity : item) {
            BillInSapDetail detail = new BillInSapDetail();
            detail.setBillInSapId(billInSap.getBillInSapId());
//            detail.setVBELN(entity.getVBELN());
//            detail.setPOSNR(entity.getPOSNR());
//            detail.setMATNR(detail.getMATNR());
//            detail.setWERKS(detail.getWERKS());
//            detail.setLGORT(detail.getLGORT());
//            detail.setLGMNG(detail.getLGMNG());
//            detail.setVOLUM(detail.getVOLUM());
//            detail.setMEINS(detail.getMEINS());
//            detail.setCHARG(detail.getCHARG());
//            detail.setITEMTEXT(detail.getITEMTEXT());
            detail.setCreateTime(DateUtil.now());
            // 订单应收数量
            //detail.setRemain(detail.getLGMNG());
            detail.setReceiptState(SapStateConst.RECEIPT_STATE_NO_RECEIVE);
            detail.setCarryState(SapStateConst.CARRY_STATE_NO);
            int res = sapDetailMapper.saveBillInSapDetail(detail);
            if (res != 1){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return false;
            }
        }
        return true;
    }

}
